<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$party                  = array();
$billDetailDebitArray   = array();
$billDetailCreditArray  = array();
$total                  = array();
$opening                = array();
$openingFinal           = array();
$partyId                = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
$bullionmasterid        = isset($_POST['party']) ? $_POST['party'] : 0;
$vepaariBookDate        = 0;
$totNetWeight           = 0;
$billBookDetailId       = 0;
$billBookNo             = 0;
$billNo                 = 0;
$showPrev               = 0;
$fromDateSelected       = isset($_REQUEST['vepaariBookDate']) ? $_REQUEST['vepaariBookDate'] : "";
$toDate                 = 0;
$drCounter              = 0;
$crCounter              = 0;
$netfine                = 0;
$netamount              = 0;
$openingFinalFineDr     = 0;
$openingFinalFineCr     = 0;
$openingFinalAmountDr   = 0;
$openingFinalAmountCr   = 0;
$total['DrFine']        = 0;
$total['DrQty']         = 0;
$total['DrLabour']      = 0;
$total['DrAmount']      = 0;
$total['CrFine']        = 0;
$total['CrQty']         = 0;
$total['CrLabour']      = 0;
$total['CrAmount']      = 0;
$partyName              = "";
$fromDate               = 0;
$vepaariDebitprevFine   = 0;
$vepaariDebitprevAmount = 0;

//if(isset($_REQUEST['fromDateYear']))
//  $fromDate = isset($_REQUEST['fromDateYear']) ? $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'] : 0;
//else
//  $fromDate  = date("Y-m-d");
//if(isset($_REQUEST['toDateYear']))
//  $toDate  = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;
//else
//  $fromDate  = date("Y-m-d");
//  
if($partyId > 0)
{
 $toDate  = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;
 $selectbillDetailArray = "SELECT vepaaribilldetail.vepaariBookMasterId,vepaaribilldetail.vepaariBookDetailId,
	                                vepaaribillmaster.vepaariBookMasterId,vepaaribillmaster.partyId,
	                                DATE_FORMAT(vepaaribillmaster.vepaariBookDate,'%Y-%m-%d') AS vepaariBookDate,
	                                vepaaribilldetail.particulars,vepaaribilldetail.netWeight,vepaaribilldetail.touch,
	                                vepaaribilldetail.waste,vepaaribilldetail.fine,vepaaribilldetail.qty,vepaaribilldetail.labour,
	                                vepaaribilldetail.amount,vepaaribilldetail.CrDr,vepaaribilldetail.rojmelId,vepaaribillmaster.prevFine,
	                                vepaaribillmaster.prevAmount
	                           FROM vepaaribilldetail
	                           JOIN vepaaribillmaster ON vepaaribilldetail.vepaariBookMasterId = vepaaribillmaster.vepaariBookMasterId
	                          WHERE vepaaribilldetail.vepaariBookMasterId = ".$_GET['vepaariBookMasterId']."
	                            AND vepaaribillmaster.partyId = ".$_GET['partyId']."
	                            AND vepaaribillmaster.vepaariBookDate <= '".$toDate."'
	                       ORDER BY vepaaribillmaster.vepaariBookDate";
	$selectbillDetailArrayRes = mysql_query($selectbillDetailArray);
	$vepaaribillDetailRow = mysql_num_rows($selectbillDetailArrayRes);

  $opening['drNetWeight'] = 0; 
  $opening['drTouch']     = 0; 
  $opening['drWaste']     = 0; 
  $opening['drTouch']     = 0; 
  $opening['drFine']      = 0; 
  $opening['drQty']       = 0; 
  $opening['drLabour']    = 0; 
  $opening['drAmount']    = 0; 
  $opening['crNetWeight'] = 0; 
  $opening['crLabour']    = 0; 
  $opening['crWaste']     = 0; 
  $opening['crTouch']     = 0; 
  $opening['crFine']      = 0; 
  $opening['crQty']       = 0; 
  $opening['crLabour']    = 0; 
  $opening['crAmount']    = 0; 
	while($vepaaribillDetailRow = mysql_fetch_array($selectbillDetailArrayRes))
	{
	  $vepaariDebitprevFine   =  $vepaaribillDetailRow['prevFine'];
	  $vepaariDebitprevAmount =  $vepaaribillDetailRow['prevAmount'];
		if($vepaaribillDetailRow['vepaariBookDate'] >= $fromDateSelected)
    {
		  if($vepaaribillDetailRow['CrDr'] == 'Dr')
	    {
	      $billDetailDebitArray[$drCounter]['vepaariBookDetailId']  = $vepaaribillDetailRow['vepaariBookDetailId'];
	      $billDetailDebitArray[$drCounter]['vepaariBookMasterId']  = $vepaaribillDetailRow['vepaariBookMasterId'];
	      $billDetailDebitArray[$drCounter]['vepaariBookDate']      = $vepaaribillDetailRow['vepaariBookDate'];
	      $billDetailDebitArray[$drCounter]['particulars']          = $vepaaribillDetailRow['particulars'];
	      $billDetailDebitArray[$drCounter]['netWeight']            = $vepaaribillDetailRow['netWeight'];
	      $billDetailDebitArray[$drCounter]['partyId']              = $vepaaribillDetailRow['partyId'];
	      $billDetailDebitArray[$drCounter]['touch']                = $vepaaribillDetailRow['touch'];
	      $billDetailDebitArray[$drCounter]['waste']                = $vepaaribillDetailRow['waste'];
	      $billDetailDebitArray[$drCounter]['fine']                 = $vepaaribillDetailRow['fine'];
	      $billDetailDebitArray[$drCounter]['qty']                  = $vepaaribillDetailRow['qty'];
	      $billDetailDebitArray[$drCounter]['labour']               = $vepaaribillDetailRow['labour'];
	      $billDetailDebitArray[$drCounter]['amount']               = $vepaaribillDetailRow['amount'];
	      $drCounter++;
	    }
	    else
	    {
	      $billDetailCreditArray[$crCounter]['vepaariBookDetailId'] = $vepaaribillDetailRow['vepaariBookDetailId'];
	      $billDetailCreditArray[$crCounter]['vepaariBookMasterId'] = $vepaaribillDetailRow['vepaariBookMasterId'];
	      $billDetailCreditArray[$crCounter]['vepaariBookDate']     = $vepaaribillDetailRow['vepaariBookDate'];
	      $billDetailCreditArray[$crCounter]['particulars']         = $vepaaribillDetailRow['particulars'];
	      $billDetailCreditArray[$crCounter]['netWeight']           = $vepaaribillDetailRow['netWeight'];
	      $billDetailCreditArray[$crCounter]['partyId']             = $vepaaribillDetailRow['partyId'];
	      $billDetailCreditArray[$crCounter]['touch']               = $vepaaribillDetailRow['touch'];
	      $billDetailCreditArray[$crCounter]['waste']               = $vepaaribillDetailRow['waste'];
	      $billDetailCreditArray[$crCounter]['fine']                = $vepaaribillDetailRow['fine'];
	      $billDetailCreditArray[$crCounter]['qty']                 = $vepaaribillDetailRow['qty'];
	      $billDetailCreditArray[$crCounter]['labour']              = $vepaaribillDetailRow['labour'];
	      $billDetailCreditArray[$crCounter]['amount']              = $vepaaribillDetailRow['amount'];
	      $crCounter++;
	    }
	  }
	  else
	  {      
	  	if($vepaaribillDetailRow['CrDr'] == 'Dr')
      {
	  	  $opening['drNetWeight'] += $vepaaribillDetailRow['netWeight'];
        $opening['drTouch']     += $vepaaribillDetailRow['touch'];
        $opening['drWaste']     += $vepaaribillDetailRow['waste'];
        $opening['drFine']      += $vepaaribillDetailRow['fine'];
        $opening['drQty']       += $vepaaribillDetailRow['qty'];
        $opening['drLabour']    += $vepaaribillDetailRow['labour'];
        $opening['drAmount']    += $vepaaribillDetailRow['amount'];   
      }
	  	else
	  	{
	  		$opening['crNetWeight'] += $vepaaribillDetailRow['netWeight'];
        $opening['crTouch']     += $vepaaribillDetailRow['touch'];
        $opening['crWaste']     += $vepaaribillDetailRow['waste'];
        $opening['crFine']      += $vepaaribillDetailRow['fine'];
        $opening['crQty']       += $vepaaribillDetailRow['qty'];
        $opening['crLabour']    += $vepaaribillDetailRow['labour'];
        $opening['crAmount']    += $vepaaribillDetailRow['amount'];
	    }
	    $openingFinal['netFineOpening'] = $opening['drFine'] - $opening['crFine'];
	    $openingFinal['netAmountOpening'] = $opening['drAmount'] - $opening['crAmount'];
	    if($openingFinal['netFineOpening'] > 0 )
	    {
	      $openingFinalFineDr = $openingFinal['netFineOpening']; 
	      $openingFinalFineCr = "";
	    }
	    else 
	    {
	      $openingFinalFineCr = $openingFinal['netFineOpening']; 
	      $openingFinalFineDr = "";
	    }
	     if($openingFinal['netAmountOpening'] > 0 )
	    {
	      $openingFinalAmountDr = $openingFinal['netAmountOpening']; 
	      $openingFinalAmountCr = "";
	    }
	    else 
	    {
	      $openingFinalAmountCr = $openingFinal['netAmountOpening']; 
	      $openingFinalAmountDr = "";
	    }
	  }
	  
	  if($vepaaribillDetailRow['CrDr'] == 'Dr')
	  {
	    $total['DrFine']      += $vepaaribillDetailRow['fine'];
	    $total['DrQty']       += $vepaaribillDetailRow['qty'];
	    $total['DrLabour']    += $vepaaribillDetailRow['labour'];
	    $total['DrAmount']    += $vepaaribillDetailRow['amount'];
	  }
	  else
	  {
	    $total['CrFine']      += $vepaaribillDetailRow['fine'];        
	    $total['CrQty']       += $vepaaribillDetailRow['qty'];         
	    $total['CrLabour']    += $vepaaribillDetailRow['labour'];      
	    $total['CrAmount']    += $vepaaribillDetailRow['amount'];       	
	  }
	   $total['totFine']   = $total['DrFine'] - $total['CrFine'];
	   $total['totAmount'] = $total['DrAmount'] - $total['CrAmount'];
	   }
	}

$selectParty = "SELECT partyId,partyName,partyFromDate 
                  FROM party
                 ORDER BY partyName";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]   = 0;
$party['partyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'];
  $party['partyName'][$a] = $partyRow['partyName'];
  $party['partyFromDate'][$a] = $partyRow['partyFromDate'];
  $a++;

if(isset($_POST['bdate']) == "1" && $partyRow['partyId'] == $_POST['party'])
  {
    $fromDate = $partyRow['partyFromDate'];
  }
}
$smarty = new Smarty;
require("bottom.php");
$smarty->assign("netfine",$netfine);
$smarty->assign("netamount",$netamount);
$smarty->assign("bullionmasterid",$bullionmasterid);
$smarty->assign("billDetailDebitArray",$billDetailDebitArray);
$smarty->assign("fromDateSelected",$fromDateSelected);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("billDetailCreditArray",$billDetailCreditArray);
$smarty->assign("total",$total);
$smarty->assign("party",$party);
$smarty->assign("partyId",$partyId);
$smarty->assign("partyName",$partyName);
$smarty->assign("toDate",$toDate);
$smarty->assign("opening",$opening);
$smarty->assign("openingFinal",$openingFinal);
$smarty->assign("openingFinalFineDr",$openingFinalFineDr);
$smarty->assign("openingFinalFineCr",$openingFinalFineCr);
$smarty->assign("openingFinalAmountDr",$openingFinalAmountDr);
$smarty->assign("openingFinalAmountCr",$openingFinalAmountCr);
$smarty->assign("vepaariDebitprevFine",$vepaariDebitprevFine);
$smarty->assign("vepaariDebitprevAmount",$vepaariDebitprevAmount);
$smarty->assign("doSubmit",(isset($_POST['fromDateDay']) ? 0 : 1));
$smarty->display("vepaariBookListNewJavak.tpl");
?>